package com.lzlg.lzlglab.web;

import com.github.pagehelper.PageInfo;
import com.lzlg.lzlglab.entity.SmartDevice;
import com.lzlg.lzlglab.service.SmartDeviceService;
import com.lzlg.lzlglab.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
@Api("设备管理模块")
public class SmartDeviceController {

    @Autowired
    SmartDeviceService smartDeviceService;

    @GetMapping("/by/condition")
    @ApiOperation("根据条件分页查询设备")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "pageNum",required = true,dataType = "query"),
            @ApiImplicitParam(value = "条数",name = "pageSize",required = true,dataType = "query"),
            @ApiImplicitParam(value = "设备名称",name = "key",dataType = "query"),
            @ApiImplicitParam(value = "设备类型",name = "typeId",dataType = "query")
    })
    public Response findDeviceByPage(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     @RequestParam(required = false) String key,
                                     @RequestParam(required = false) String typeId){
        PageInfo<SmartDevice> allByPage = smartDeviceService.findAllByPage(pageNum, pageSize, key, typeId);
        return Response.ok(allByPage);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增/修改设备")
    public Response saveOrUpdateDevice(@RequestBody SmartDevice smartDevice){
        smartDeviceService.saveOrUpdate(smartDevice);
        return Response.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一条设备")
    public Response removeDeviceById(@PathVariable String id){
        smartDeviceService.deleteById(id);
        return Response.ok();
    }
}
